﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;

using Antlr.StringTemplate;
using Core.Wapper.CSharpWapper;
using DAL;

namespace Core
{
    public class Builder
    {
        public TableWrapper Table = null;

        private SchemaHeler m_SchemaHeler = null;
        
        public Builder(string providerName, string connectionString, string tableName, string[] tableRestriction)
        {
            this.m_SchemaHeler = new SchemaHeler(providerName, connectionString);
            this.Table = new TableWrapper(this.m_SchemaHeler, tableName, tableRestriction);
        }

        public void SetAttribute(StringTemplate stringTemplate, TableWrapper tableWrapper)
        {
            stringTemplate.SetAttribute("table", tableWrapper);
            stringTemplate.SetAttribute("allColumns", tableWrapper.Columns);
            
            stringTemplate.SetAttribute("pkColumns", tableWrapper.PkColumns); 
            stringTemplate.SetAttribute("nonPkColumns", tableWrapper.NonPkColumns);

            stringTemplate.SetAttribute("idColumn", tableWrapper.IdentityColumns);
            stringTemplate.SetAttribute("nonIdColumns", tableWrapper.NonIdentityColumns);
            
            stringTemplate.SetAttribute("updateColumns", tableWrapper.UpdateColumns);
        }

        public string GetTempateValue(string templateFileFullPath)
        {
            var stg = new StringTemplateGroup("myGroup", Path.GetDirectoryName(templateFileFullPath));
            StringTemplate st = stg.GetInstanceOf(Path.GetFileNameWithoutExtension(templateFileFullPath));
            this.SetAttribute(st, Table);
            return st.ToString();
        }
    }
}
